<script setup>
import { ref, reactive, shallowRef, shallowReactive, markRaw, toRaw, isProxy, isReactive, isRef, isShallow } from 'vue'
import CodeBlock from '@components/CodeBlock.vue'

// 基础响应式数据
const normalRef = ref({ count: 0 })
const normalReactive = reactive({ count: 0 })

// 浅层响应式数据
const shallowRefData = shallowRef({ nested: { count: 0 } })
const shallowReactiveData = shallowReactive({ nested: { count: 0 } })

// 非响应式数据
const rawObject = { name: '非响应式对象', data: { value: 42 } }
const markedRawObject = markRaw(rawObject)
const reactiveWithRaw = reactive({ name: '响应式容器', raw: markedRawObject })

// 转换演示
const proxyObject = reactive({ id: 1, name: '代理对象' })
const rawFromProxy = toRaw(proxyObject)

// 状态检查结果
const stateChecks = {
  isProxyNormalReactive: false,
  isReactiveNormalReactive: false,
  isRefNormalRef: false,
  isShallowShallowRef: false,
  isShallowShallowReactive: false,
  isProxyMarkedRaw: false
}

// 更新计数器方法
const incrementNormalRef = () => {
  normalRef.value.count++
}

const incrementNormalReactive = () => {
  normalReactive.count++
}

// 浅层响应式更新方法
const updateShallowRefTop = () => {
  shallowRefData.value = { nested: { count: shallowRefData.value.nested.count + 1 } }
}

const updateShallowRefNested = () => {
  shallowRefData.value.nested.count++
}

const updateShallowReactiveTop = () => {
  shallowReactiveData.count = (shallowReactiveData.count || 0) + 1
}

const updateShallowReactiveNested = () => {
  shallowReactiveData.nested.count++
}

// 非响应式更新方法
const updateRawObject = () => {
  markedRawObject.data.value++
}

// 执行状态检查
const performChecks = () => {
  stateChecks.isProxyNormalReactive = isProxy(normalReactive)
  stateChecks.isReactiveNormalReactive = isReactive(normalReactive)
  stateChecks.isRefNormalRef = isRef(normalRef)
  stateChecks.isShallowShallowRef = isShallow(shallowRefData)
  stateChecks.isShallowShallowReactive = isShallow(shallowReactiveData)
  stateChecks.isProxyMarkedRaw = isProxy(markedRawObject)
}

// 代码示例
const shallowRefCode = `// 浅层Ref - 仅顶层属性响应式
const shallowRefData = shallowRef({ nested: { count: 0 } })

// 直接替换整个value会触发更新
shallowRefData.value = { nested: { count: 1 } }

// 嵌套属性修改不会触发更新
shallowRefData.value.nested.count++ // 不会触发UI更新`

const shallowReactiveCode = `// 浅层Reactive - 仅顶层属性响应式
const shallowReactiveData = shallowReactive({ nested: { count: 0 } })

// 顶层属性修改会触发更新
shallowReactiveData.count = 1

// 嵌套属性修改不会触发更新
shallowReactiveData.nested.count++ // 不会触发UI更新`

const markRawCode = `// markRaw - 创建不可被转为响应式的对象
const rawObject = { name: '非响应式对象', data: { value: 42 } }
const markedRawObject = markRaw(rawObject)

// 即使放入响应式对象中，markedRawObject仍然是非响应式的
const reactiveWithRaw = reactive({ 
  name: '响应式容器', 
  raw: markedRawObject 
})

// 修改markedRawObject不会触发UI更新
markedRawObject.data.value++`

const toRawCode = `// toRaw - 获取响应式代理对应的原始对象
const proxyObject = reactive({ id: 1, name: '代理对象' })
const rawFromProxy = toRaw(proxyObject)

// 修改原始对象不会触发UI更新
rawFromProxy.name = '修改后的名称'

// 但原始对象和代理对象指向相同的内存
console.log(proxyObject.name) // '修改后的名称'`

const isAPICode = `// 检查响应式状态的工具函数
const checks = {
  isProxy: isProxy(reactiveObject),        // 检查是否是Proxy
  isReactive: isReactive(reactiveObject),  // 检查是否是响应式对象
  isRef: isRef(refObject),                // 检查是否是Ref
  isShallow: isShallow(shallowObject)      // 检查是否是浅层响应式
}`

// 最佳实践提示
const bestPractices = [
  { title: '性能优化', content: '对于大型对象或频繁修改的非响应式数据，使用shallowRef或shallowReactive可以减少依赖追踪开销' },
  { title: '第三方库集成', content: '使用markRaw可以防止将复杂的第三方库实例转换为响应式对象，避免潜在的兼容性问题' },
  { title: '临时修改', content: '使用toRaw获取原始对象进行批量修改，可以减少触发不必要的更新' },
  { title: '注意事项', content: '浅层响应式容易导致状态不一致，使用时需要确保只修改被追踪的顶层属性' }
]
</script>

<template>
  <div class="reactive-advanced-demo">
    <h1 class="demo-title">Vue 3 高级响应式API演示</h1>
    
    <!-- 功能介绍 -->
    <div class="intro-section">
      <p>本演示展示Vue 3中的高级响应式API，帮助您理解和掌握更精细的响应式控制。</p>
      <p>这些API适用于性能优化和特殊场景的状态管理需求。</p>
    </div>

    <!-- 基础对比 -->
    <div class="demo-section">
      <h2>1. 基础响应式 vs 浅层响应式</h2>
      
      <div class="comparison-grid">
        <!-- 正常响应式 -->
        <div class="demo-card">
          <h3>正常响应式</h3>
          
          <div class="demo-item">
            <h4>ref</h4>
            <p>当前值: {{ normalRef.value.count }}</p>
            <button @click="incrementNormalRef" class="demo-button">增加</button>
          </div>
          
          <div class="demo-item">
            <h4>reactive</h4>
            <p>当前值: {{ normalReactive.count }}</p>
            <button @click="incrementNormalReactive" class="demo-button">增加</button>
          </div>
        </div>
        
        <!-- 浅层响应式 -->
        <div class="demo-card">
          <h3>浅层响应式</h3>
          
          <div class="demo-item">
            <h4>shallowRef</h4>
            <p>嵌套值: {{ shallowRefData.value.nested.count }}</p>
            <button @click="updateShallowRefTop" class="demo-button">更新顶层</button>
            <button @click="updateShallowRefNested" class="demo-button warning">更新嵌套</button>
            <small class="hint">更新嵌套不会触发UI变化</small>
          </div>
          
          <div class="demo-item">
            <h4>shallowReactive</h4>
            <p>嵌套值: {{ shallowReactiveData.nested.count }}</p>
            <button @click="updateShallowReactiveNested" class="demo-button warning">更新嵌套</button>
            <small class="hint">更新嵌套不会触发UI变化</small>
          </div>
        </div>
      </div>
      
      <!-- 代码示例 -->
      <div class="code-examples">
        <div class="code-section">
          <h3>shallowRef 示例</h3>
          <CodeBlock :code="shallowRefCode" language="javascript" />
        </div>
        
        <div class="code-section">
          <h3>shallowReactive 示例</h3>
          <CodeBlock :code="shallowReactiveCode" language="javascript" />
        </div>
      </div>
    </div>

    <!-- markRaw 演示 -->
    <div class="demo-section">
      <h2>2. markRaw - 非响应式对象</h2>
      
      <div class="demo-card">
        <div class="demo-item">
          <h4>非响应式对象演示</h4>
          <p>原始值: {{ markedRawObject.data.value }}</p>
          <button @click="updateRawObject" class="demo-button warning">修改值</button>
          <small class="hint">即使修改值，UI也不会更新，因为对象被标记为非响应式</small>
        </div>
        
        <div class="demo-item">
          <h4>在响应式容器中</h4>
          <p>容器名称: {{ reactiveWithRaw.name }}</p>
          <p>原始对象值: {{ reactiveWithRaw.raw.data.value }}</p>
          <small class="hint">即使在响应式对象内部，markedRaw对象仍然保持非响应式</small>
        </div>
      </div>
      
      <div class="code-section">
        <h3>markRaw 示例</h3>
        <CodeBlock :code="markRawCode" language="javascript" />
      </div>
    </div>

    <!-- toRaw 演示 -->
    <div class="demo-section">
      <h2>3. toRaw - 获取原始对象</h2>
      
      <div class="demo-card">
        <div class="demo-item">
          <h4>代理对象 vs 原始对象</h4>
          <p>代理对象名称: {{ proxyObject.name }}</p>
          <p>原始对象名称: {{ rawFromProxy.name }}</p>
          <p>是否指向相同对象: {{ proxyObject === rawFromProxy ? '是' : '否' }}</p>
          <small class="hint">修改原始对象会改变代理对象的值，但不会触发UI更新</small>
        </div>
      </div>
      
      <div class="code-section">
        <h3>toRaw 示例</h3>
        <CodeBlock :code="toRawCode" language="javascript" />
      </div>
    </div>

    <!-- 状态检查 -->
    <div class="demo-section">
      <h2>4. 响应式状态检查</h2>
      
      <div class="demo-card">
        <button @click="performChecks" class="demo-button primary">执行检查</button>
        
        <div class="check-results" v-if="Object.values(stateChecks).some(v => v !== false)">
          <div class="check-item" v-for="(value, key) in stateChecks" :key="key">
            <strong>{{ key }}:</strong> {{ value ? '是' : '否' }}
          </div>
        </div>
      </div>
      
      <div class="code-section">
        <h3>isXxx API 示例</h3>
        <CodeBlock :code="isAPICode" language="javascript" />
      </div>
    </div>

    <!-- 最佳实践 -->
    <div class="demo-section">
      <h2>5. 最佳实践</h2>
      
      <div class="practices-grid">
        <div class="practice-card" v-for="practice in bestPractices" :key="practice.title">
          <h3>{{ practice.title }}</h3>
          <p>{{ practice.content }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.reactive-advanced-demo {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.demo-title {
  color: #333;
  margin-bottom: 1.5rem;
  text-align: center;
}

.intro-section {
  background-color: #f8f9fa;
  padding: 1.5rem;
  border-radius: 8px;
  margin-bottom: 2rem;
  text-align: center;
  line-height: 1.6;
}

.demo-section {
  margin-bottom: 3rem;
}

.demo-section h2 {
  color: #42b883;
  margin-bottom: 1.5rem;
  border-bottom: 2px solid #42b883;
  padding-bottom: 0.5rem;
}

.demo-section h3 {
  color: #333;
  margin-bottom: 1rem;
}

.comparison-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
  margin-bottom: 2rem;
}

.demo-card {
  background-color: white;
  border-radius: 8px;
  padding: 1.5rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.demo-item {
  margin-bottom: 1.5rem;
  padding: 1rem;
  background-color: #f8f9fa;
  border-radius: 6px;
}

.demo-item:last-child {
  margin-bottom: 0;
}

.demo-button {
  background-color: #42b883;
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
  margin-right: 0.5rem;
  margin-bottom: 0.5rem;
  transition: background-color 0.2s;
}

.demo-button:hover {
  background-color: #38a169;
}

.demo-button.primary {
  background-color: #3b82f6;
}

.demo-button.primary:hover {
  background-color: #2563eb;
}

.demo-button.warning {
  background-color: #f59e0b;
}

.demo-button.warning:hover {
  background-color: #d97706;
}

.hint {
  display: block;
  margin-top: 0.5rem;
  color: #666;
  font-size: 0.85rem;
}

.code-examples {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
}

.code-section {
  background-color: white;
  border-radius: 8px;
  padding: 1.5rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.check-results {
  margin-top: 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.check-item {
  background-color: #f8f9fa;
  padding: 0.5rem 1rem;
  border-radius: 4px;
}

.practices-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.5rem;
}

.practice-card {
  background-color: white;
  border-radius: 8px;
  padding: 1.5rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border-left: 4px solid #42b883;
}

.practice-card h3 {
  color: #42b883;
  margin-bottom: 0.5rem;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .comparison-grid,
  .code-examples {
    grid-template-columns: 1fr;
  }
  
  .reactive-advanced-demo {
    padding: 1rem;
  }
}
</style>